﻿using System;
using System.Collections.Generic;

namespace Sendz.Domain
{
    // Skipping data annotations and validation in this class since it's primarily a data cross-reference class
    public class Response
    {
        // Entity
        public Guid ResponseKey { get; set; }
        public DateTime CreatedOn { get; set; }

        // Parents      
        public Guid FormKey { get; set;  }
        public Form Form { get; set; }

        // (Optional)
        public Guid? IdentityKey { get; set; } // Nullable, a response doesn't have to have an email identity field
        public Identity Identity { get; set; }

        public Guid? AutoResponseKey { get; set; } // Nullable, a response doesn't have to have an autoresponse (an autoresponder is optional on the form)
        public AutoResponse AutoResponse { get; set; }

        // Children
        public List<ResponseItem> ResponseItems { get; set; }
       
        // Constructor
        public Response()
        {
            // Moved to a calculated column in the Entity Framework ORM (property StoreGeneratedPattern)
            // Ref: http://msdn.microsoft.com/en-us/library/ms189786.aspx
            // Ref: http://leedumond.com/blog/using-a-guid-as-an-entitykey-in-entity-framework-4/
            // this.ResponseKey = Guid.NewGuid();
            this.CreatedOn = DateTime.Now;
        }

        public Response(Guid formKey)
        {
            // Moved to a calculated column in the Entity Framework ORM (property StoreGeneratedPattern)
            // this.ResponseKey = Guid.NewGuid();
            this.FormKey = formKey;
            this.CreatedOn = DateTime.Now;
        }

        public Response(Guid identityKey, Guid formKey, string autoResponseKey = null)
        {
            // Moved to a calculated column in the Entity Framework ORM (property StoreGeneratedPattern)
            // this.ResponseKey = Guid.NewGuid();
            this.IdentityKey = identityKey;
            var autoResponseKeyGuid = Guid.Empty;
            if (Guid.TryParse(autoResponseKey, out autoResponseKeyGuid))
                this.AutoResponseKey = autoResponseKeyGuid;
            this.FormKey = formKey;
            this.CreatedOn = DateTime.Now;
        }
    }
}
